package com.xhwl.data.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.CustomOrganizationType;
import com.xhwl.common.pojo.cent.sdata.Organization;
import com.xhwl.common.query.cent.sdata.CustomOrganizationTypeQuery;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.data.service.ICustomOrganizationTypeService;
import com.xhwl.data.service.IOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;


/**
 * (CustomOrganizationType)表控制层
 *
 * @author makejava
 * @since 2023-05-31 10:27:41
 */
@RestController
public class CustomOrganizationTypeController {
    @Autowired
    private ICustomOrganizationTypeService customOrganizationTypeService;
    @Autowired
    private IOrganizationService organizationService;

    @RequestMapping(value = "/org/custom-type/insert")
    public ResultJson insert(@RequestBody CustomOrganizationType customOrganizationType){
        if(StringUtils.isEmpty(customOrganizationType.getName()) || customOrganizationType.getName().length() > 24 || customOrganizationType.getName().length()<2){
            return ResultJson.fail("名称输入错误");
        }
        if (null == customOrganizationType.getEnterpriseId()){
            return ResultJson.fail("企业不能为空");
        }
        QueryWrapper<CustomOrganizationType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id",customOrganizationType.getEnterpriseId());
        queryWrapper.eq("name",customOrganizationType.getName());
        if (null != customOrganizationType.getId()){
            queryWrapper.ne("id",customOrganizationType.getId());
        }
        List<CustomOrganizationType> list = customOrganizationTypeService.list(queryWrapper);
        if (!list.isEmpty()){
            return ResultJson.fail("名称重复");
        }
        if (null == customOrganizationType.getId()){
            String code = UUID.randomUUID().toString().replace("-","").substring(0,8);
            customOrganizationType.setCode(code);
            customOrganizationTypeService.save(customOrganizationType);
        }else {
            CustomOrganizationType old = customOrganizationTypeService.getById(customOrganizationType.getId());
            if (null == old){
                return ResultJson.fail("类型不存在");
            }
            customOrganizationType.setCode(old.getCode());
            customOrganizationTypeService.updateById(customOrganizationType);
        }
        return ResultJson.success();
    }

    @RequestMapping(value = "/org/custom-type/list")
    public ResultJson list(Integer enterpriseId){
        QueryWrapper<CustomOrganizationType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id",enterpriseId);
        return ResultJson.success(customOrganizationTypeService.list(queryWrapper));
    }

    @RequestMapping(value = "/org/custom-type/page")
    public ResultJson page(@RequestBody CustomOrganizationTypeQuery query, Page page){
        QueryWrapper<CustomOrganizationType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id",query.getEnterpriseId());
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(query.getKeyword())){
            queryWrapper.like("name",query.getKeyword());
        }
        return ResultJson.success(customOrganizationTypeService.page(page,queryWrapper));
    }

    @RequestMapping(value = "/org/custom-type/delete")
    public ResultJson delete(Integer id){
        CustomOrganizationType customOrganizationType = customOrganizationTypeService.getById(id);
        if (null == customOrganizationType){
            return ResultJson.fail("组织类型不存在");
        }
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("custom_org_type",id);
        List<Organization> organizationList = organizationService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(organizationList)){
           return ResultJson.fail("当前类型被引用，无法删除！");
        }
        boolean b = customOrganizationTypeService.removeById(id);
        return ResultJson.success("删除成功", b);
    }
    @RequestMapping(value = "/org/custom-type/detail")
    public ResultJson detail(Integer id){
        return ResultJson.success(customOrganizationTypeService.getById(id));
    }
}

